<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function myPromise(excuter) {
            let self = this;
            this.status = 'pending'
            this.value = null;
            this.reason = null;

            this.onFulfilledCallback = [];
            this.onRejectedCallback = [];

            function resolve(value) {

                if (self.status == 'pending') {
                    self.status = 'fulfilled';
                    self.value = value;
                    self.onFulfilledCallback.forEach(item => {
                        item(value);
                    })
                }
            }

            function reject(reason) {
                if (self.status == 'pending') {
                    self.status = 'rejected';
                    self.reason = reason;
                    sel.onRejectedCallback.forEach(item => {
                        item(reason)
                    })
                }
            }

            try {
                excuter(resolve, reject)
            } catch (e) {
                reject(e)
            }

        }

        myPromise.prototype.then = function (onFulFilled, onRejected) {
            onFulFilled = typeof onFulFilled === 'function' ? onFulFilled : function (data) {
                resolve(data);
            }
            onRejected = typeof onRejected === 'function' ? onRejected : function (err) {
                throw err;
            }
            let _this = this;
            if (_this.status === 'pending') {
                _this.onFulfilledCallback.push(onFulFilled)
                _this.onRejectedCallback.push(onRejected);
            }

            if (_this.status == 'fulfilled') {
                onFulFilled(this.value)
            } else if (_this.status == 'rejected') {
                onRejected(this.reason)
            }
        }

        function promiseAll(arr) {
            return new Promise((resolve, reject) => {
                if (!Array.isArray(arr)) {
                    return reject(new Error('传入的不是数组！'))
                }
                let ans = []
                const len = arr.length;
                let counter = 0;
                for (let i = 0; i < len; i++) {
                    Promise.resolve(arr[i]).then(res => {
                        counter++;
                        ans[i] = res;
                        if (counter === len) {
                            resolve(ans);
                        }
                    }).catch(e => {
                        reject(e)
                    })
                }
            })

        }

        let demo1 = new myPromise((resolve, reject) => {
            resolve(123);
        })
        demo1.then(data => console.log(data))
        let demo2 = promiseAll([6])
        demo2.then(res => {
            console.log(res)
        })
    </script>
</body>

</html>